package org.study.shell;

import java.util.ArrayList;
import java.util.List;

public class ShellBase {
    /**
     * 防火墙添加端口
     *
     * @param port 端口
     */
    public static List<String> firewallAddPort(int port) {
        List<String> commands = new ArrayList<String>();
        commands.add("# 防火墙配置");
        commands.add("echo \"防火墙允许" + port + "端口通过\"");
        commands.add("sudo firewall-cmd --add-port=" + port + "/tcp --permanent");
        commands.add("sudo firewall-cmd --reload");
        return commands;
    }

    /**
     * 防火墙添加端口
     *
     * @param ports 多个端口启动
     */
    public static List<String> firewallAddPorts(String ports) {
        List<String> commands = new ArrayList<String>();
        commands.add("# 防火墙配置");
        commands.add("echo \"防火墙允许" + ports + "端口通过\"");
        String[] portSplit = ports.split(",");
        for (String port : portSplit) {
            commands.add("sudo firewall-cmd --add-port=" + port + "/tcp --permanent");
        }
        commands.add("sudo firewall-cmd --reload");
        return commands;
    }

    public static List<String> addProfile(List<String> list, String fileName) {
        List<String> commands = new ArrayList<String>();
//        commands.add("#将环境变量写入到/etc/profile.d/xxx.sh");
        commands.add("sudo echo \"#环境变量\" > /etc/profile.d/" + fileName);
        for (String command : list) {
            commands.add("sudo echo \"" + command + "\" >> /etc/profile.d/" + fileName);
        }
        commands.add("#环境变量生效");
        commands.add("cat /etc/profile.d/" + fileName);
        commands.add("source /etc/profile");
        return commands;
    }

    /**
     * 命令执行是否正常， 异常程序不退出
     *
     * @param success 执行成功输出语句
     * @param error   执行失败输出语句
     */
    public static List<String> command(String success, String error) {
        return command(success, error, false);
    }

    /**
     * 命令执行是否正常
     *
     * @param success 执行成功输出语句
     * @param error   执行失败输出语句
     * @param exit    执行异常，是否退出程序：true: 退出,false：不退出
     */
    public static List<String> command(String success, String error, boolean exit) {
        List<String> commands = new ArrayList<String>();
        commands.add("if [ $? -eq 0 ]; then");
        commands.add("  echo \"----------------------------------------\"");
        commands.add("  echo \"" + success + "\"");
        commands.add("else");
        commands.add("  echo \"----------------------------------------\"");
        commands.add("  echo \"" + error + "\"");
        commands.add("  if " + exit + "; then");
        commands.add("     exit 1");
        commands.add("  fi");
        commands.add("fi");
        return commands;
    }

    /**
     * 修改hostname，需要重启机器生效
     *
     * @param hostname 新的hostname
     */
    public static String changeHostname(String hostname) {
        return "sudo hostnamectl set-hostname " + hostname;
    }

    /**
     * 重启机器
     */
    public static String reboot() {
        return "sudo reboot";
    }
}
